package zmee.pmct.formplugin.pmas;

import dm.jdbc.util.StringUtil;
import kd.bos.coderule.api.CodeRuleInfo;
import kd.bos.dataentity.entity.DynamicObject;
import kd.bos.dataentity.entity.DynamicObjectCollection;
import kd.bos.entity.datamodel.ListSelectedRow;
import kd.bos.entity.datamodel.ListSelectedRowCollection;
import kd.bos.form.control.events.BeforeItemClickEvent;
import kd.bos.form.control.events.ItemClickEvent;
import kd.bos.list.BillList;
import kd.bos.list.plugin.AbstractListPlugin;
import kd.bos.orm.query.QCP;
import kd.bos.orm.query.QFilter;
import kd.bos.servicehelper.BusinessDataServiceHelper;
import kd.bos.servicehelper.QueryServiceHelper;
import kd.bos.servicehelper.coderule.CodeRuleServiceHelper;
import kd.bos.servicehelper.operation.SaveServiceHelper;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;

/**
 * @Author: zyj
 * @Date: 2023/6/15 16:53
 * @Description: 项目财务预算: 更新EAS项目总投资预算数据到项目立项单的财务概算和项目概算中
 */
public class UpdateProjectBudgetListPlugin extends AbstractListPlugin {

    // 更新项目预算按钮标识
    private static final String BUTTON_KEY_UPDATEPROJECTBUDGET = "zmee_updateprojectbudget";
    // 存放提示信息
    private StringBuilder messageStr;

    @Override
    public void beforeItemClick(BeforeItemClickEvent evt) {
        super.beforeItemClick(evt);
        // 点击更新项目预算按钮时
        if (StringUtil.equalsIgnoreCase(BUTTON_KEY_UPDATEPROJECTBUDGET, evt.getItemKey())) {
            BillList list = this.getControl(BILLLISTID);
            ListSelectedRowCollection coll = list.getSelectedRows();
            // 判断是否选中行
            if (coll == null || coll.size() <= 0) {
                this.getView().showTipNotification("请至少选择一行数据！");

            }
        }
    }

    @Override
    public void itemClick(ItemClickEvent evt) {
        super.itemClick(evt);
        // 点击更新项目预算按钮时
        if (StringUtil.equalsIgnoreCase(BUTTON_KEY_UPDATEPROJECTBUDGET, evt.getItemKey())) {
            BillList list = this.getControl(BILLLISTID);
            String billFormId = list.getBillFormId();
            ListSelectedRowCollection selectedRows = list.getSelectedRows();
            ArrayList<DynamicObject> dynamicObjects = new ArrayList<>();
            ArrayList<DynamicObject> projectUpdates = new ArrayList<>();
            messageStr = new StringBuilder();
            for (ListSelectedRow selectedRow : selectedRows) {
                Object primaryKeyValue = selectedRow.getPrimaryKeyValue();
                // 获得表单数据
                DynamicObject dynamicObjectObj = BusinessDataServiceHelper.loadSingle(primaryKeyValue, billFormId);
                // 单据状态
                String billstatus = dynamicObjectObj.getString("billstatus");
                if (!"C".equals(billstatus)) {
                    messageStr.append("单据编号:"+dynamicObjectObj.getString("billno")+"未审核,更新失败!");
                    continue;
                }
                // 获取申报年度
                Date date = dynamicObjectObj.getDate("date");
                Calendar cal = Calendar.getInstance();
                cal.setTime(date);
                // 获取申报年度年份
                int year = cal.get(Calendar.YEAR);
                Calendar calnow = Calendar.getInstance();
                // 获取当前年份
                int yearnow = calnow.get(Calendar.YEAR);
                if (year != yearnow) {
                    messageStr.append("单据编号:"+dynamicObjectObj.getString("billno")+"申报年度不为"+yearnow+",更新失败!");
                    continue;
                }
                // EAS项目总投资预算
                BigDecimal budgetnum = dynamicObjectObj.getBigDecimal("budgetnum");
                // 获取项目
                DynamicObject projectname = dynamicObjectObj.getDynamicObject("projectname");
                if (projectname == null) {
                    messageStr.append("单据编号:"+dynamicObjectObj.getString("billno")+"项目为空!更新失败!");
                    continue;
                }
                // 获取项目编码
                String projectNumber = projectname.getString("number");
                QFilter qFilter = new QFilter("pro.number", QCP.equals, projectNumber);
                // 查询项目立项单
                DynamicObjectCollection queryIds = QueryServiceHelper.query("pmas_pro_approval", "id", new QFilter[]{qFilter});
                if (queryIds == null || queryIds.size() != 1) {
                    messageStr.append("单据编号:"+dynamicObjectObj.getString("billno")+"查出的项目立项单为空或者多个更新失败!");
                    continue;
                }
                // 项目立项单
                DynamicObject projectApprova = BusinessDataServiceHelper.loadSingle(queryIds.get(0).get("id"), "pmas_pro_approval");
                // 项目预算
                projectApprova.set("projcetbugamt", budgetnum);
                // 财务预算
                projectApprova.set("zmee_textfield1", budgetnum);
                // 创建项目调整单对象
                DynamicObject projectUpdate = BusinessDataServiceHelper.newDynamicObject("pmas_pro_approvaladjust");
                // 调用自动编码规则
                CodeRuleInfo codeRule = CodeRuleServiceHelper.getCodeRule(projectUpdate.getDataEntityType().getName(), projectUpdate, null);
                String number = CodeRuleServiceHelper.getNumber(codeRule, projectUpdate);
                // 单据编号
                projectUpdate.set("billno", number);
                // 项目名称
                projectUpdate.set("pro", projectApprova.getDynamicObject("pro"));
                // 项目名称
                projectUpdate.set("proname", projectApprova.getDynamicObject("pro").get("name"));
                // 上级项目
                projectUpdate.set("parentpro", projectApprova.getDynamicObject("parentpro"));
                // 项目分类
                projectUpdate.set("kind", projectApprova.getDynamicObject("kind"));
                // 业务组织
                projectUpdate.set("org", projectApprova.getDynamicObject("org"));
                // 实施组织
                projectUpdate.set("department", projectApprova.getDynamicObject("org"));
                // 管理组织
                projectUpdate.set("syscreateorg", projectApprova.getDynamicObject("org"));
                // 核算组织
                projectUpdate.set("accountorg", projectApprova.getDynamicObject("org"));
                // 调整说明
                projectUpdate.set("zmee_textareafield", "EAS预算更新");
                // 单据状态
                projectUpdate.set("billstatus", "C");
                // 制单时间
                projectUpdate.set("createtime", new Date());
                // 更改类型
                projectUpdate.set("zmee_combofield", "02");
                // 项目概算总额
                projectUpdate.set("zmee_decimalfield", budgetnum);
                dynamicObjects.add(projectApprova);
                projectUpdates.add(projectUpdate);
            }
            SaveServiceHelper.save(dynamicObjects.toArray(new DynamicObject[0]));
            SaveServiceHelper.save(projectUpdates.toArray(new DynamicObject[0]));
        }
        if (messageStr != null) {
            if (messageStr.length() != 0) {
                this.getView().showMessage(String.valueOf(messageStr));
            }
        }
    }
}
